﻿using System;
using System.Windows.Input;

namespace TransControlSystem.ViewModel.Common.Command
{
    /// <summary>
    /// 委托工具类
    /// </summary>
    public class DelegateCommand : ICommand
    {
        /// <summary>
        /// 条件事件
        /// </summary>
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public Predicate<object> CanExecuteFunc { set; get; }
        /// <summary>
        /// 有参触发 
        /// </summary>
        public Action<object> ExecuteActionObj { set; get; }
        /// <summary>
        /// 无参触发
        /// </summary>
        public Action ExecuteAction { set; get; }

        public bool CanExecute(object parameter)
        {
            return CanExecuteFunc == null ? true : CanExecuteFunc(parameter);
        }

        public void Execute(object parameter)
        {
            if (ExecuteAction != null)
                ExecuteAction.Invoke();
            else if (ExecuteActionObj != null)
                ExecuteActionObj(parameter);
        }
    }
}
